package com.lsj.controller;

import cn.hutool.core.convert.Convert;
import com.lsj.core.page.QueryPage;
import com.lsj.core.page.ResultPage;
import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.core.validate.Add;
import com.lsj.core.validate.Update;
import com.lsj.dto.sys.RoleDto;
import com.lsj.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:lin.sj
 * @Data:2021/7/8 21:32
 * @Desc:
 */
@RestController
@RequestMapping("/sys_role")
@Api(tags = "角色处理")
public class RoleController {
    @Autowired
    RoleService roleService;

    @PostMapping("/add")
    @ApiOperation(value = "增加角色")
    public CommonResult addRole(@RequestBody @Validated(Add.class)RoleDto dto){
        RoleDto roleDto = roleService.addRole(dto);
        return  CommonResult.success(roleDto);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    public CommonResult deleteRole(@RequestBody Map param){
        Long roleId = Convert.toLong(param.get("roleId"));
        ExceptionHelperUtil.nullObjException(roleId,"角色ID不能为空");
        roleService.deleteRole(roleId);
        return  CommonResult.success();
    }
    @PostMapping("/update")
    @ApiOperation(value = "更新角色")
    public CommonResult updateRole(@RequestBody @Validated(Update.class)RoleDto dto){
        roleService.updateRole(dto);
        return  CommonResult.success();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取角色")
    public CommonResult getRole(@RequestParam("roleId") Long roleId){
        ExceptionHelperUtil.nullObjException(roleId,"角色ID不能为空");
        return CommonResult.success(roleService.getRoleById(roleId));
    }


    @PostMapping("/list")
    @ApiOperation(value = "获取角色列表")
    public CommonResult listRole(@RequestBody QueryPage<RoleDto> queryPage){
        ResultPage<RoleDto> page = roleService.listRole(queryPage);
        return CommonResult.success(page);
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "获取所有角色列表")
    public CommonResult listAllRole(){
        return CommonResult.success(roleService.listAllRole());
    }

}
